package com.group9.handinhand.dao.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.springframework.beans.BeanUtils;

import com.group9.handinhand.service.so.AbstractBaseSO;

@MappedSuperclass
public abstract class AbstractBaseModel implements Serializable {
	
	private static final long serialVersionUID = 1L;

	@Id
	@Column(name = "ID", nullable = false)
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long id;
	
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "CREATE_DATE" , nullable = false)
    private Date createDate = new Date();

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "UPDATE_DATE", nullable = false)
    private Date updateDate = new Date();

	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Date getCreateDate() {
		return createDate;
	}
	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	public Date getUpdateDate() {
		return updateDate;
	}
	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
	}
	
	@Override
	public String toString() {
		return "AbstractBaseModel [id=" + id + ", createDate=" + createDate
				+ ", updateDate=" + updateDate + "]";
	}
	
	protected <T extends AbstractBaseSO> T convert(T so) {
		return convert(so, null);
	}

	protected <T extends AbstractBaseSO> T convert(T so, String[] ignoreProperties) {
		BeanUtils.copyProperties(this, so, ignoreProperties);
		return so;
	}
	
    @Override
    public boolean equals(Object obj) {
	if (this == obj)
	    return true;
	if (obj == null)
	    return false;
	if (!(obj instanceof AbstractBaseModel))
	    return false;
	AbstractBaseModel other = (AbstractBaseModel) obj;
	if (getId() == null) {
		return false;
	} else if (!getId().equals(other.getId())) {
	    return false;
	}
	return true;
    }
	

}
